﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Dragon.Library.Framework.Entity.Search.Page
{
    public sealed class PageResult<T> where T : class, new()
    {
        public IList<T> Data { get; set; }

        ///<summary>
        /// 总页数
        ///</summary>
        public int PageCount
        {
            get
            {
                int remainder = RecordCount % PageSize;
                int pageTotal = RecordCount / PageSize;
                if (remainder > 0)
                    pageTotal++;
                if (pageTotal <= 0)
                    pageTotal = 1;
                return pageTotal;
            }
        }

        ///<summary>
        /// 总行数
        ///</summary>
        public int RecordCount { get; set; }

        /// <summary>
        /// 每页大小
        /// </summary>
        public int PageSize { get; set; }

        public PageResult()
        {
            Data = new List<T>();
        }

        /// <summary>
        /// 获得指定位置的实体类
        /// </summary>
        /// <param name="position"></param>
        /// <returns></returns>
        public T GetEntity(int position)
        {
            return Data[position];
        }

        ///<summary>
        ///</summary>
        ///<param name="index"></param>
        public T this[int index]
        {
            get { return Data[index]; }
            set { Data[index] = value; }
        }

        /// <summary>
        /// 添加实体类
        /// </summary>
        /// <param name="entity"></param>
        public void Add(T entity)
        {
            if (entity != null)
            {
                Data.Add(entity);
            }
        }
        /// <summary>
        /// 清空实体类集合
        /// </summary>
        public void ClearEntities()
        {
            Data.Clear();
        }
        /// <summary>
        /// 获得实体集合数量
        /// </summary>
        /// <returns></returns>
        public int Count()
        {
            return Data.Count;
        }

        ///<summary>
        ///</summary>
        ///<param name="entities"></param>
        public void SetEntities(IList<T> entities)
        {
            Data = new List<T>(entities);
        }

        ///<summary>
        ///</summary>
        ///<returns></returns>
        public IList<T> GetEntries()
        {
            return new List<T>(Data);
        }

        public override string ToString()
        {
            var result = new StringBuilder();
            foreach (T entity in Data)
            {
                result.Append(entity);
                result.Append(Environment.NewLine);
            }

            return result.ToString();
        }


        
    }
}
